package com.codingke.codingbookstore.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.codingke.codingbookstore.App;
import com.codingke.codingbookstore.R;
import com.codingke.codingbookstore.adapter.base.ViewHolder;
import com.codingke.codingbookstore.utils.NumberUtils;
import com.codingke.codingbookstore.vo.BookInfo;

import java.util.ArrayList;

/**
 * descreption:
 * company: codingke.com
 * Created by vince on 15/11/16.
 */
public class BookListItemAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<BookInfo> bookInfos;
    public BookListItemAdapter(Context context,ArrayList<BookInfo> bookInfos){
        this.context = context;
        this.bookInfos = bookInfos;
    }

    @Override
    public int getCount() {
        return bookInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return bookInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.book_list_item,null);
        }
        TextView tv_bookName = ViewHolder.getView(convertView, R.id.textView_bookName,null);
        TextView textView3_star  = ViewHolder.getView(convertView, R.id.textView3_star,null);
        TextView textView5_price  = ViewHolder.getView(convertView, R.id.textView5_price,null);
        TextView textView7_discountPrice  = ViewHolder.getView(convertView, R.id.textView7_discountPrice,null);
        TextView textView8_discount  = ViewHolder.getView(convertView, R.id.textView8_discount,null);
        ImageView imageView_bookImage = ViewHolder.getView(convertView, R.id.imageView_bookImage,null);

        BookInfo bookInfo = bookInfos.get(position);
        tv_bookName.setText(bookInfo.getBookName());
        textView3_star.setText(bookInfo.getStar()+"星");
        textView5_price.setText("原价：￥" + NumberUtils.format(bookInfo.getPrice()));
        textView5_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//给文本内容设置删除线样式
        textView7_discountPrice.setText("￥" + NumberUtils.format(bookInfo.getDiscountPrice()));
        textView8_discount.setText(NumberUtils.format(bookInfo.getDiscount())+"折");
        //显示图片
        App.bitmapUtils.display(imageView_bookImage,bookInfo.getBookImage().getFileUrl(context));
        return convertView;
    }

    public ArrayList<BookInfo> getBookInfos() {
        return bookInfos;
    }

    public void setBookInfos(ArrayList<BookInfo> bookInfos) {
        this.bookInfos = bookInfos;
    }
}














